//
//  CustomAlertView.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/17.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "CustomAlertView.h"

static CustomAlertView* wrhAlertView;
@interface CustomAlertView()
@property(nonatomic,strong)UIView* cover;//背景覆盖
@property(nonatomic,strong)UIView* baseView;//白色的底部view

@end

@implementation CustomAlertView

+(instancetype)alertViewWithTitle:(NSString*)title messageView:(UIView*)messageView ButtonsWithSureButtonTitle:(NSString*)sureButtonTitle andButtonsWithCancelButtonTitle:(NSString*)cancelButtonTitle
{
    if (wrhAlertView==nil) {
        wrhAlertView=[[self alloc]init];
    }
    if (wrhAlertView.cover==nil) {
        UIView*cover=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        cover.backgroundColor=[UIColor blackColor];//WRHColor(150, 150, 150);
        cover.alpha=0.3;
        [[UIApplication sharedApplication].keyWindow addSubview:cover];
        wrhAlertView.cover=cover;
    }
    if (wrhAlertView.baseView==nil) {
        //2.底部白色的view
        UIView*baseView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.8, 130+messageView.frame.size.height)];
        baseView.backgroundColor=[UIColor whiteColor];
        baseView.layer.cornerRadius=10;
        baseView.layer.masksToBounds=YES;
        baseView.center = wrhAlertView.cover.center;
        wrhAlertView.baseView=baseView;
        [[UIApplication sharedApplication].keyWindow addSubview:baseView];
        
    }
    [wrhAlertView setTitle:title messageView:messageView ButtonsWithSureButtonTitle:sureButtonTitle andButtonsWithCancelButtonTitle:cancelButtonTitle];
    
    return  wrhAlertView;
}

-(void)setTitle:(NSString*)title messageView:(UIView*)messageView ButtonsWithSureButtonTitle:(NSString*)sureButtonTitle andButtonsWithCancelButtonTitle:(NSString*)cancelButtonTitle
{
  
    //只做两个按钮的情况 取消+确定
    CGFloat xFrame = 28;
//    if (IPHONE6)
//    {
//        xFrame = 34;
//    }
//    if (IPHONE6P)
//    {
//        xFrame = 40;
//    }
    //确定按钮
    UIButton *sureButton = [[UIButton alloc]initWithFrame:CGRectMake(xFrame, self.baseView.frame.size.height - 50, 80, 30)];
    [sureButton setTitle:sureButtonTitle forState:UIControlStateNormal];
    [sureButton setBackgroundImage:[UIImage imageNamed:@"kaiqi"] forState:UIControlStateNormal];
    sureButton.tag = 0;
    [sureButton addTarget:self action:@selector(customViewForButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [wrhAlertView.baseView addSubview:sureButton];
    
    //第二个按钮
    UIButton *sureButton2 = [[UIButton alloc]initWithFrame:CGRectMake(self.baseView.frame.size.width-xFrame-80, self.baseView.frame.size.height - 50, 80, 30)];
    [sureButton2 setBackgroundImage:[UIImage imageNamed:@"quxiao"] forState:UIControlStateNormal];
    sureButton2.tag = 1;
    [sureButton2 addTarget:self action:@selector(customViewForButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [sureButton2 setTitle:cancelButtonTitle forState:UIControlStateNormal];
    [wrhAlertView.baseView addSubview:sureButton2];
    
    //创建头部
    UIView *myAlertTopView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, self.baseView.frame.size.width, 30)];
    //创建提示语
    UILabel *myTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(myAlertTopView.frame.size.width/2-25, 0, 50, 30)];
    myTitleLabel.text = @"提示";
    [myAlertTopView addSubview:myTitleLabel];
    //创建线条视图
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(myTitleLabel.frame)-65-3, myAlertTopView.frame.size.height/2-2, 62, 2)];
    leftImageView.image = [UIImage imageNamed:@"huitiao"];
    [myAlertTopView addSubview:leftImageView];
    
    //创建线条视图
    UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myTitleLabel.frame)-5, myAlertTopView.frame.size.height/2-2, 62, 2)];
    rightImageView.image = [UIImage imageNamed:@"huitiao"];
    [myAlertTopView addSubview:rightImageView];
    
    [self.baseView addSubview:myAlertTopView];
    
    //提示框内容视图
    CGRect messageViewRect = messageView.frame;
    messageViewRect.origin.y = CGRectGetMaxY(myAlertTopView.frame)+10;
    messageView.frame = messageViewRect;
    [self.baseView addSubview:messageView];
    self.cover.hidden=NO;
    self.baseView.hidden=NO;
    
}

+(void)dissmiss{
    [wrhAlertView dissmiss];
}
-(void)dissmiss{
    [self.cover removeFromSuperview];
    self.cover=nil;
    [self.baseView removeFromSuperview];
    self.baseView=nil;
    
}
-(void)willMoveToSuperview:(UIView *)newSuperview{
    if (self.cover!=nil) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.cover];
    }
    
    
}

#pragma mark -- 按钮点击 传送委托
- (void)customViewForButtonClickAction:(UIButton *)btn
{
    self.cover.hidden=YES;
    self.baseView.hidden=YES;
    if (_delegate && [_delegate respondsToSelector:@selector(alertView:didClickButtonAtIndex:)]) {
        [_delegate alertView:self didClickButtonAtIndex:btn.tag];
    }
}

@end
